package com.example.tliasmanagement.service.impl;

import com.example.tliasmanagement.aop.MyLog;
import com.example.tliasmanagement.dto.AddDeptDto;
import com.example.tliasmanagement.dto.UpdateDeptDto;
import com.example.tliasmanagement.entity.Dept;
import com.example.tliasmanagement.entity.Student;
import com.example.tliasmanagement.mapper.DeptMapper;
import com.example.tliasmanagement.service.DeptService;
import com.example.tliasmanagement.vo.DeptVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    //@MyLog
    public List<DeptVo> depts() {
        // 从数据库查询数据了
        return deptMapper.selectAll();
    }

    @Override
    //@MyLog
    public void deleteById(Integer id) {
        deptMapper.deleteById(id);
    }

    @Override
    public void addDept(AddDeptDto dto) {
        //TODO 判断名称是否重复
        // 保存到数据库
        Dept dept = new Dept();
        dept.setName(dto.getName());
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }
    @Override
    public Dept getById(Integer id) {
        Dept dept = deptMapper.selectById(id);
        return dept;
    }

    @Override
    public void updateDept(UpdateDeptDto dto) {
        //TODO 1. 判断名称是否重复
        //2. 更新到数据库
        Dept dept = new Dept();
        dept.setName(dto.getName());
        dept.setUpdateTime(LocalDateTime.now());
        dept.setId(dto.getId());
        deptMapper.update(dept);
    }

}